﻿Imports MostRecentlyUsed.Controls
Imports System.Text

Friend Class frmDisplayProperties
    Public Sub New(ByVal propertiesCollection As TFSArtifactInfoCollection)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        If (propertiesCollection Is Nothing) Then Return

        For Each oProperties As List(Of TFSArtifactInfo) In propertiesCollection

            Dim oServerPath As TFSArtifactInfo = oProperties.FirstOrDefault(Function(oProp) String.Compare("Server path", oProp.PropertyName, True) = 0)

            If (oServerPath Is Nothing) Then Continue For

            For Each oProperty As TFSArtifactInfo In oProperties
                lvResults.Items.Add(New ListViewItem(New String() {oProperty.PropertyName, oProperty.PropertyValue},
                                                     GetGroup(Path.GetFileName(oServerPath.PropertyValue), oServerPath.PropertyValue)))
            Next
        Next

        If (lvResults.Groups.Count > 1) Then
            btnCollapseAll_Click(Nothing, Nothing)
        Else
            btnExpandAll_Click(Nothing, Nothing)
        End If

        For Each lvg As ListViewGroup In lvResults.Groups
            lvResults.SetGroupFooter(lvg, lvg.Items.Count & " properties...")
        Next

        ' HeaderSize does it based on header and content
        lvResults.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
    End Sub

    Private Function GetGroup(ByVal groupName As String, ByVal groupKey As String) As ListViewGroup
        groupName = groupName.Trim()

        For Each oExistingGroup As ListViewGroup In lvResults.Groups
            If (String.Compare(oExistingGroup.Header, groupName, True) = 0) Then
                Return oExistingGroup
            End If
        Next

        Dim oGroup As ListViewGroup = New ListViewGroup(groupKey, groupName)
        oGroup.HeaderAlignment = HorizontalAlignment.Left
        lvResults.Groups.Add(oGroup)
        Return (oGroup)
    End Function

    Private Sub btnOkay_Click(sender As Object, e As EventArgs) Handles btnOkay.Click
        Hide()
    End Sub

    Private Sub btnExpandAll_Click(sender As Object, e As EventArgs) Handles btnExpandAll.Click
        Try
            lvResults.SetGroupState(ListViewGroupState.Collapsible Or ListViewGroupState.Normal)
        Catch
        End Try
    End Sub

    Private Sub btnCollapseAll_Click(sender As Object, e As EventArgs) Handles btnCollapseAll.Click
        Try
            lvResults.SetGroupState(ListViewGroupState.Collapsible Or ListViewGroupState.Collapsed)
        Catch
        End Try
    End Sub

    Private Sub lvResults_KeyDown(sender As Object, e As KeyEventArgs) Handles lvResults.KeyDown
        Try
            If (e.KeyCode = Keys.C) And (e.Modifiers = Keys.Control) Then
                CopyListViewToClipboard(lvResults)
            End If
        Catch
        End Try
    End Sub

    Public Sub CopyListViewToClipboard(ByVal inputListBox As ListView)
        Dim oSzText As New StringBuilder

        For iCount As Integer = 0 To inputListBox.SelectedItems.Count - 1
            Dim oListViewItem As ListViewItem = inputListBox.SelectedItems(iCount)
            oSzText.AppendFormat("{0}: {1}{2}", oListViewItem.Text, oListViewItem.SubItems(1).Text, Environment.NewLine)
        Next

        My.Computer.Clipboard.SetText(oSzText.ToString)
    End Sub
End Class